package com.yidu.hbats.service.TrainingPlan;

import com.github.pagehelper.PageHelper;
import com.yidu.hbats.action.vo.AtsPlanYearMorevo;
import com.yidu.hbats.dao.AtsPlanYearMoreMapper;
import com.yidu.hbats.model.*;
import com.yidu.springbootdemo.permission.dao.TuserMapper;
import com.yidu.springbootdemo.permission.domain.Tuser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @program: hbats
 * @description 多年年度计划的服务实现类
 * @author: 周佳佳
 * @create: 2019-10-30 11:08
 **/
@Service
public class AtsPlanYearMoreServiceImpl implements AtsPlanYearMoreService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    AtsPlanYearMoreMapper atsPlanYearMoreMapper;
    //训练指标
    @Resource
    AtsPlanItemService atsPlanItemService;
    //运动员和计划的接口
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    @Resource
    TuserMapper managerModelMapper;

    @Override
    public int count(HttpServletRequest request, AtsPlanYearMore atsPlanYearMore) {

        HttpSession session = request.getSession();
        Tuser manager = (Tuser) session.getAttribute("user");
        int count = 0;
        if(manager.getCompany()!=null && !"".equals(manager.getCompany())){
            if (manager.getCompanyNum()=="2"||"2".equals(manager.getCompanyNum())) {
                atsPlanYearMore.setCoachName(manager.getCompanyName());
                atsPlanYearMore.setCoachId(Integer.valueOf(manager.getFilId()));
                count = atsPlanYearMoreMapper.countCoach(atsPlanYearMore);
            } else if (manager.getCompanyNum()=="4"||"4".equals(manager.getCompanyNum())) {
                atsPlanYearMore.setAthleteName(manager.getCompanyName());
                atsPlanYearMore.setAthleteId(Integer.valueOf(manager.getFilId()));
                count = atsPlanYearMoreMapper.countAthlete(atsPlanYearMore);
            } else {
                count = atsPlanYearMoreMapper.countAll(atsPlanYearMore);
            }
        } else {
            count = atsPlanYearMoreMapper.countAll(atsPlanYearMore);
        }
        return count;
    }

    @Override
    public List<AtsPlanYearMore> selectAll(HttpServletRequest request, AtsPlanYearMore atsPlanYearMore, Integer pageNum, Integer pageSize) {

        HttpSession session = request.getSession();
        Tuser manager = (Tuser) session.getAttribute("user");
        PageHelper.startPage(pageNum, pageSize);
        List<AtsPlanYearMore> list = null;
        if(manager.getCompany()!=null && !"".equals(manager.getCompany())){
            if (manager.getCompanyNum()=="2"||"2".equals(manager.getCompanyNum())) {
                atsPlanYearMore.setCoachName(manager.getCompanyName());
                atsPlanYearMore.setCoachId(Integer.valueOf(manager.getFilId()));
                list = atsPlanYearMoreMapper.findCoach(atsPlanYearMore);
            } else if (manager.getCompanyNum()=="4"||"4".equals(manager.getCompanyNum())) {
                atsPlanYearMore.setAthleteName(manager.getCompanyName());
                atsPlanYearMore.setAthleteId(Integer.valueOf(manager.getFilId()));
                list = atsPlanYearMoreMapper.findAthlete(atsPlanYearMore);
            } else {
                list = atsPlanYearMoreMapper.selectAll(atsPlanYearMore);
            }
        } else {
            list = atsPlanYearMoreMapper.selectAll(atsPlanYearMore);
        }


        return list;
    }

    @Override
    public List<AtsPlanYearMore> selectAllandPage() {
        return atsPlanYearMoreMapper.selectAllandPage();
    }

    @Override
    public void insertSelective(AtsPlanYearMorevo atsPlanYearMorevo) {
        AtsPlanYearMore atsPlanYearMore = new AtsPlanYearMore();
        Date utilDate = new Date();//util.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        atsPlanYearMorevo.setCreatTime(sqlDate);
        atsPlanYearMore.setSportId(atsPlanYearMorevo.getSportId());
        BeanUtils.copyProperties(atsPlanYearMorevo, atsPlanYearMore);
        atsPlanYearMoreMapper.insertSelective(atsPlanYearMore);
        AtsPlanItem atsPlanItem = new AtsPlanItem();
        atsPlanItem.setRemark("5");
        atsPlanItem.setCreater(atsPlanYearMorevo.getCreater());
        atsPlanItem.setIsva(1);
        atsPlanItem.setCreatTime(sqlDate);
        //多年年度计划传1 靠这个区别和查询
        atsPlanItem.setPlandate("1");
        atsPlanItem.setPid(atsPlanYearMore.getPid());
        atsPlanItem.setPname(atsPlanYearMorevo.getPname());
        boolean f = atsPlanYearMorevo.getItemNameone().contains(",");
        if (f == true) {
            String[] arr = atsPlanYearMorevo.getItemNameone().split(",");
            String[] arrtwo = atsPlanYearMorevo.getItemValueone().split(",");
            for (int i = 0; i < arr.length; i++) {
                String s = arr[i];
                atsPlanItem.setItemName(s);
                String sa = arrtwo[i];
                atsPlanItem.setItemValue(sa);
                atsPlanItemService.insertSelective(atsPlanItem);
            }

        } else {
            atsPlanItem.setItemName(atsPlanYearMorevo.getItemNameone());
            atsPlanItem.setItemValue(atsPlanYearMorevo.getItemValueone());
            atsPlanItemService.insertSelective(atsPlanItem);
        }
        //new出计划教练员关联表
        AtsEmpAthleteScheme atsEmpAthlete = new AtsEmpAthleteScheme();
        atsEmpAthlete.setCreater(atsPlanYearMorevo.getCreater());
        //传入5代表多年训练计划
        atsEmpAthlete.setRemark("5");
        atsEmpAthlete.setPname(atsPlanYearMorevo.getPname());
        atsEmpAthlete.setPid(atsPlanYearMore.getPid());
        boolean flag = atsPlanYearMorevo.getAthleteId().contains(",");
        if (flag == true) {
            String[] athleteId = atsPlanYearMorevo.getAthleteId().split(",");
            String[] athleteName = atsPlanYearMorevo.getAthleteName().split(",");
            for (int i = 0; i < athleteId.length; i++) {
                String athleteIdone = athleteId[i];
                String athleteNameone = athleteName[i];
                atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                atsEmpAthlete.setAthleteName(athleteNameone);
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        } else {
            atsEmpAthlete.setAthleteId(Integer.valueOf(atsPlanYearMorevo.getAthleteId()));
            atsEmpAthlete.setAthleteName(atsPlanYearMorevo.getAthleteName());
            atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
        }
    }
    @Override
    public int delAll(String id) {
        String[] ids = id.split(",");
        int rows=0;

        for(int i = 0;i<ids.length;i++){
            //删除计划同时删除下面指标
            AtsPlanItem atsPlanItem=new AtsPlanItem();
            atsPlanItem.setIsva(0);
            atsPlanItem.setPid(Integer.valueOf(ids[i]));
            //删除对应的运动员
            AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
            atsEmpAthleteScheme.setIsva(0);
            atsEmpAthleteScheme.setPid(Integer.valueOf(ids[i]));

            AtsPlanYearMore atsPlanYearMore=new AtsPlanYearMore();
            atsPlanYearMore.setPid(Integer.valueOf(ids[i]));
            atsPlanYearMore.setIsva(0);
            rows=updateIsva(atsPlanYearMore);
            atsEmpAthleteSchemeService.updatePid(atsEmpAthleteScheme);
            atsPlanItemService.updateOrdelete(atsPlanItem);
            rows++;
        }
        return rows;
    }
    @Override
    public int updateIsva(AtsPlanYearMore atsPlanYearMore) {
        atsPlanYearMore.setIsva(0);
        int rows = atsPlanYearMoreMapper.updateIsva(atsPlanYearMore);
        return rows;
    }

    @Override
    public AtsPlanYearMore selectByPrimaryKey(Integer pid) {
        AtsPlanYearMore atsPlanYearMore = atsPlanYearMoreMapper.selectByPrimaryKey(pid);
        return atsPlanYearMore;
    }

    @Override
    public void updateByPrimaryKeySelective(AtsPlanYearMorevo atsPlanYearMorevo) {
        AtsPlanYearMore atsPlanYearMore = new AtsPlanYearMore();
        Date utilDate = new Date();//util.Date
        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
        atsPlanYearMorevo.setCreatTime(sqlDate);
        BeanUtils.copyProperties(atsPlanYearMorevo, atsPlanYearMore);
        atsPlanYearMoreMapper.updateByPrimaryKeySelective(atsPlanYearMore);
        AtsPlanItem atsPlanItemt = new AtsPlanItem();
        atsPlanItemt.setIsva(0);
        atsPlanItemt.setPid(atsPlanYearMorevo.getPid());
        int rows = atsPlanItemService.updateOrdelete(atsPlanItemt);
        AtsEmpAthleteScheme atsEmpAthleteScheme = new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setPid(atsPlanYearMorevo.getPid());
        int rowsone = atsEmpAthleteSchemeService.updatePid(atsEmpAthleteScheme);
        if (rowsone > 0) {
            //new出计划教练员关联表
            AtsEmpAthleteScheme atsEmpAthlete = new AtsEmpAthleteScheme();
            atsEmpAthlete.setCreater(atsPlanYearMorevo.getCreater());
            //传入5代表多年训练计划
            atsEmpAthlete.setRemark("5");
            atsEmpAthlete.setPname(atsPlanYearMorevo.getPname());
            atsEmpAthlete.setPid(atsPlanYearMore.getPid());
            boolean flag = atsPlanYearMorevo.getAthleteId().contains(",");
            if (flag == true) {
                String[] athleteId = atsPlanYearMorevo.getAthleteId().split(",");
                String[] athleteName = atsPlanYearMorevo.getAthleteName().split(",");
                for (int i = 0; i < athleteId.length; i++) {
                    String athleteIdone = athleteId[i];
                    String athleteNameone = athleteName[i];
                    atsEmpAthlete.setAthleteId(Integer.valueOf(athleteIdone));
                    atsEmpAthlete.setAthleteName(athleteNameone);
                    atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
                }
            } else {
                atsEmpAthlete.setAthleteId(Integer.valueOf(atsPlanYearMorevo.getAthleteId()));
                atsEmpAthlete.setAthleteName(atsPlanYearMorevo.getAthleteName());
                atsEmpAthleteSchemeService.insertSelective(atsEmpAthlete);
            }
        }
        if (rows > 0) {
            AtsPlanItem atsPlanItemone = new AtsPlanItem();
            atsPlanItemone.setRemark("5");
            atsPlanItemone.setCreater(atsPlanYearMorevo.getCreater());
            atsPlanItemone.setIsva(1);
            atsPlanItemone.setCreatTime(sqlDate);
            //多年年度计划传1 靠这个区别和查询
            atsPlanItemone.setPlandate("1");
            atsPlanItemone.setPid(atsPlanYearMore.getPid());
            atsPlanItemone.setPname(atsPlanYearMorevo.getPname());
            //查询传过来得字符有没有， 有就会返回一个true
            boolean f = atsPlanYearMorevo.getItemNameone().contains(",");
            if (f == true) {
                String[] arr = atsPlanYearMorevo.getItemNameone().split(",");
                String[] arrtwo = atsPlanYearMorevo.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    String sa = arrtwo[i];
                    atsPlanItemone.setItemName(s);
                    atsPlanItemone.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItemone);
                }
            } else {
                atsPlanItemt.setCreater(atsPlanYearMorevo.getCreater());
                atsPlanItemt.setPname(atsPlanYearMorevo.getPname());
                atsPlanItemone.setItemName(atsPlanYearMorevo.getItemNameone());
                atsPlanItemone.setItemValue(atsPlanYearMorevo.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItemone);
            }
        } else {
            AtsPlanItem atsPlanItemtwo = new AtsPlanItem();
            atsPlanItemtwo.setRemark("5");
            atsPlanItemtwo.setCreater(atsPlanYearMorevo.getCreater());
            atsPlanItemtwo.setIsva(1);
            atsPlanItemtwo.setCreatTime(sqlDate);
            //多年年度计划传1 靠这个区别和查询
            atsPlanItemtwo.setPlandate("1");
            atsPlanItemtwo.setPid(atsPlanYearMore.getPid());
            atsPlanItemtwo.setPname(atsPlanYearMorevo.getPname());
            //查询传过来得字符有没有， 有就会返回一个true
            boolean f = atsPlanYearMorevo.getItemNameone().contains(",");
            if (f == true) {
                String[] arr = atsPlanYearMorevo.getItemNameone().split(",");
                String[] arrtwo = atsPlanYearMorevo.getItemValueone().split(",");
                for (int i = 0; i < arr.length; i++) {
                    String s = arr[i];
                    String sa = arrtwo[i];
                    atsPlanItemtwo.setItemName(s);
                    atsPlanItemtwo.setItemValue(sa);
                    atsPlanItemService.insertSelective(atsPlanItemtwo);
                }
            } else {
                atsPlanItemtwo.setItemName(atsPlanYearMorevo.getItemNameone());
                atsPlanItemtwo.setItemValue(atsPlanYearMorevo.getItemValueone());
                atsPlanItemService.insertSelective(atsPlanItemtwo);
            }
        }
    }

    @Override
    public AtsPlanYearMore findMaxPid(int coachId) {
        return atsPlanYearMoreMapper.findMaxPid(coachId);
    }

}
